package com.zdong.http;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyLog;
import com.zdong.http.expand.Volley_ex;

/**
 *  @version 1.0
 *
 */
public class HttpUtil{
    private static RequestQueue requestQueue;
    /**
     * 初始化
     * @param context
     */
    public  static void  start(Context context){
        start(context, null);
    }
    public static  void  start(Context context,String cachename){
            requestQueue = Volley_ex.newRequestQueue_Okhttp(context);
        if (!TextUtils.isEmpty(cachename)) {
            Volley_ex.DEFAULT_CACHE_DIR = cachename;
        }
        requestQueue.addRequestFinishedListener(new RequestQueue.RequestFinishedListener<String>() {
            @Override
            public void onRequestFinished(Request<String> request) {
                Log.e("url",request.toString());
            }
        });
        VolleyLog.DEBUG=true;
        VolleyLog.TAG="zoudong";
        requestQueue.start();
    }

    public static  RequestQueue getRequestQueue() {
        if (requestQueue==null)
           throw   new RuntimeException("未初始化，请先调用start");
        return requestQueue;
    }

    public static <T> void get(Request<T> request){
        getRequestQueue().add(request);
    }
    public static void cancelAll(Object tag){
        getRequestQueue().cancelAll(tag);
    }
    public static void cancelAll(RequestQueue.RequestFilter requestFilter){
        getRequestQueue().cancelAll(requestFilter);
    }

    public static void  stop(){
        getRequestQueue().stop();
        requestQueue=null;
    }

    public static Long getDiskCacheSize() {
        return Volley_ex.diskCache.getTotalSize();
    }
    public static void clearDiskCache() {
         Volley_ex.diskCache.clear();
    }
}
